//
//  PictureView.swift
//  ZQWeibo
//
//  Created by summer的Dad on 16/11/17.
//  Copyright © 2016年 summer的Dad. All rights reserved.
//

import UIKit
private let cellID = "PictureID"

class PictureView: UICollectionView {
/*
     @interface SDPhotoBrowser : UIView <UIScrollViewDelegate>
     
     @property (nonatomic, weak) UIView *sourceImagesContainerView;
     @property (nonatomic, assign) NSInteger currentImageIndex;
     @property (nonatomic, assign) NSInteger imageCount;
     
     @property (nonatomic, weak) id<SDPhotoBrowserDelegate> delegate;
     
     - (void)show;
     
     @end
     
     */


    
    var pictureInfo : [ZQPictureInfo]?{
        
        didSet{
           
            self.reloadData()
        }
   }
    
    //为什么在这
    override func awakeFromNib() {
        
        let nib = UINib(nibName: "PictureViewCell", bundle: nil)
       self.register(nib, forCellWithReuseIdentifier: cellID)
        
        self.delegate = self
        
        self.dataSource = self
        
        
    }

}
extension PictureView : UICollectionViewDataSource ,UICollectionViewDelegate{
    
    //实现数据源方法
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return pictureInfo?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath) as! PictureViewCell
        
        let model = self.pictureInfo![indexPath.item]
        
        cell.pictureInfo = model
        
       return cell
    }
  
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        
        
        //实例化SDPhOTOBrowser
        
        let browser = SDPhotoBrowser()
        
        browser.sourceImagesContainerView = self
        
        browser.currentImageIndex = indexPath.item
        
        browser.imageCount = pictureInfo?.count ?? 0
        
        
        browser.delegate = self
        
        browser.show()
        
       
        
        
    }
    
    
    
   
    
}







extension PictureView: SDPhotoBrowserDelegate {
    
    //实现代理方法
    func photoBrowser(_ browser: SDPhotoBrowser!, highQualityImageURLFor index: Int) -> URL! {
        //高清
        let prcture = self.pictureInfo![index]
        let url = URL(string: prcture.bimiddleimage ?? "")
        
        return url
        
        
    }
    
    func photoBrowser(_ browser: SDPhotoBrowser!, placeholderImageFor index: Int) -> UIImage! {
        
        let indexPath = IndexPath(item: index, section: 0)
        
        let cell = self.cellForItem(at: indexPath) as! PictureViewCell
        
        return cell.image_Gif.image
        
        
    }

    
    
    
    
}

/*
 - (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index;
 
 @optional
 
 - (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index;
 
 @end
 */



